package com.tpf.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tpf.common.utils.PageUtils;
import com.tpf.common.utils.Query;
import com.tpf.mall.product.dao.CategoryDao;
import com.tpf.mall.product.entity.CategoryEntity;
import com.tpf.mall.product.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<CategoryEntity> page = this.page(
				new Query<CategoryEntity>().getPage(params),
				new QueryWrapper<CategoryEntity>()
		);

		return new PageUtils(page);
	}

	@Override
	public List<CategoryEntity> listWithTree() {
		// 1查询所有分类
		List<CategoryEntity> list = baseMapper.selectList(null);

		// 2组装父子的树形结构

		// 2.1 找到所有的一级分类
		List<CategoryEntity> level1Menus = list.stream().filter((categoryEntity) ->
				categoryEntity.getParentCid() == 0
		).map((menu)->{
			menu.setChildren(null);
			return menu;
		}).collect(Collectors.toList());


		return level1Menus;
	}

}